package cn.com.fuhuitong.main.order.ui.activity

import androidx.lifecycle.Observer
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.main.base.ViewModeActivity
import cn.com.fuhuitong.main.order.vm.OrderViewModel
import com.alibaba.android.arouter.facade.annotation.Route
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.base.EmptyContent
import cn.com.fuhuitong.entity.MainBean
import cn.com.fuhuitong.entity.OrderAction
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.ItemViewType
import cn.com.fuhuitong.main.order.adapter.*
import cn.com.fuhuitong.main.order.entity.OrderDetailsEntity
import cn.com.fuhuitong.main.order.ui.fragment.OrderListFragment
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import com.alibaba.android.vlayout.layout.SingleLayoutHelper
import kotlinx.android.synthetic.main.activity_order_details.*
import org.greenrobot.eventbus.EventBus

@Route(path = ARouteConstance.ACTIVITY_URL_ORDER_DETAILS)
class OrderDetailsActivity : ViewModeActivity<OrderViewModel>() {

    @Autowired(name = AppConstance.BUNDLE_ORDER_ID)
    @JvmField
    var orderId: Int = 0

    private lateinit var detailsStateAdapter: OrderDetailsStateAdapter

    private lateinit var detailsGoodsHeadsAdapter: OrderDetailsGoodsHeadsAdapter

    private lateinit var detailsGoodsAdapter: OrderDetailsGoodsAdapter

    private lateinit var detailsNatureAdapter: OrderDetailsNatureAdapter

    private lateinit var detailsAddressAdapter: OrderDetailsAddressAdapter

    private lateinit var detailsOtherAdapter: OrderDetailsOtherAdapter

    override fun viewModel(): OrderViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_order_details

    override fun initView() {

        image_order_details_back.setOnClickListener {
            this.finish()
        }

        this.initViewRecyclerAdapter(
            recycler_order_details, arrayOf(
                ItemViewType.ORDER_DETAILS_STATE,
                ItemViewType.ORDER_DETAILS_GOODS_HEADS,
                ItemViewType.ORDER_DETAILS_GOODS,
                ItemViewType.ORDER_DETAILS_NATURE,
                ItemViewType.ORDER_DETAILS_ADDRESS,
                ItemViewType.ORDER_DETAILS_OTHER
            )
        )

        delegateAdapter.addAdapters(mutableListOf<DelegateAdapter.Adapter<*>>(
            OrderDetailsStateAdapter(this, SingleLayoutHelper().apply {
                paddingTop = resources.getDimensionPixelSize(R.dimen.dp_0)
            }).apply {
                detailsStateAdapter = this
            },
            OrderDetailsGoodsHeadsAdapter(this, SingleLayoutHelper().apply {
                paddingTop = resources.getDimensionPixelSize(R.dimen.dp_10)
            }).apply {
                detailsGoodsHeadsAdapter = this
            },
            OrderDetailsGoodsAdapter(this, LinearLayoutHelper().apply {
                paddingTop = resources.getDimensionPixelSize(R.dimen.dp_0)
            }).apply {
                detailsGoodsAdapter = this
            },
            OrderDetailsNatureAdapter(this, SingleLayoutHelper().apply {
                paddingTop = resources.getDimensionPixelSize(R.dimen.dp_10)
            }).apply {
                detailsNatureAdapter = this
            },
            OrderDetailsAddressAdapter(this, SingleLayoutHelper().apply {
                paddingTop = resources.getDimensionPixelSize(R.dimen.dp_10)
            }).apply {
                detailsAddressAdapter = this
            },
            OrderDetailsOtherAdapter(this, SingleLayoutHelper().apply {
                paddingTop = resources.getDimensionPixelSize(R.dimen.dp_10)
            }).apply {
                detailsOtherAdapter = this
            }
        ))

        detailsStateAdapter.setOnItemChildClickListener { view, i, orderDetailsEntity ->
            when (view.id) {
                R.id.text_order_details_cancel -> {
                    viewModel.orderCancel(orderDetailsEntity.id)
                }
                R.id.text_order_details_pay -> {
                    viewModel.orderPay(orderDetailsEntity.id)
                }
                R.id.text_order_details_take -> {
                    viewModel.orderConfirm(orderDetailsEntity.id)
                }
            }
        }

    }

    override fun initViewMode() {
        viewModel.orderDetailsDataLiveData.observe(this, Observer { response ->

            val dataLength: Int = if (response.response?.data == null) 0 else 1

            when (response.status) {
                HttpResponseStatus.Status.START -> {
                    if (dataLength == 0) {
                        showLoadEmptyView()
                    }
                }
                HttpResponseStatus.Status.SUCCESS -> {
                    if (dataLength == 0) {
                        showNullEmptyView(EmptyContent("重新加载", "暂无数据"))
                    } else {
                        hideEmptyView()
                    }
                }
                HttpResponseStatus.Status.FAILURE -> {
                    if (dataLength == 0) {
                        showErrorEmptyView(EmptyContent("重新加载", response.statusTip))
                    }
                    toast(response.statusTip)
                }
            }

            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                this.fillData(it)
            }
        })

        viewModel.baseDataLiveData.observe(this, Observer { response ->
            handlerResponseLoading(response.status, response.statusTip)
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            toast(response.response?.msg ?: "操作成功")
            viewModel.orderDetail(orderId)
        })

        viewModel.payDataLiveData.observe(this, Observer { response ->
            handlerResponseLoading(response.status, response.statusTip)
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_ORDER_PAY_WAIT)
                    .withSerializable(AppConstance.BUNDLE_ORDER_PAY, it)
                    .navigation()
            }
        })
    }

    override fun initData() {
        viewModel.orderDetail(orderId)
    }

    private fun fillData(entity: OrderDetailsEntity) {
        detailsStateAdapter.setAddDataNew(mutableListOf(entity))
        detailsGoodsHeadsAdapter.setAddDataNew(mutableListOf(entity))
        detailsGoodsAdapter.setAddDataNew(entity.goods_list)
        detailsNatureAdapter.setAddDataNew(mutableListOf(entity))
        detailsAddressAdapter.setAddDataNew(mutableListOf(entity))
        detailsOtherAdapter.setAddDataNew(mutableListOf(entity))
    }
}